package com.campuscarbooking.api.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.campuscarbooking.api.model.Review;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.time.LocalDateTime;
import java.util.List;

@Mapper
public interface ReviewMapper extends BaseMapper<Review> {

    /**
     * 查询指定司机的评价信息
     */
    @Select("SELECT r.* FROM reviews r WHERE r.driver_id = #{driverId} AND r.review_status = 'approved'")
    IPage<Review> findApprovedReviewsByDriverId(Page<?> page, @Param("driverId") Long driverId);

    /**
     * 统计指定司机的平均评分
     */
    @Select("SELECT AVG((r.service_rating + r.comfort_rating + r.punctuality_rating) / 3.0) " +
            "FROM reviews r WHERE r.driver_id = #{driverId} AND r.review_status = 'approved'")
    Double calculateDriverAverageRating(@Param("driverId") Long driverId);

    /**
     * 查询指定用户的评价信息
     */
    @Select("SELECT r.* FROM reviews r WHERE r.user_id = #{userId}")
    IPage<Review> findByUserId(Page<?> page, @Param("userId") Long userId);

    /**
     * 查询指定预约的评价信息
     */
    @Select("SELECT r.* FROM reviews r WHERE r.reservation_id = #{reservationId}")
    Review findByReservationId(@Param("reservationId") Long reservationId);

    /**
     * 查询指定时间段内的评价信息
     */
    @Select("SELECT r.* FROM reviews r WHERE r.created_at >= #{startTime} AND r.created_at <= #{endTime}")
    List<Review> findByCreatedAtBetween(
            @Param("startTime") LocalDateTime startTime,
            @Param("endTime") LocalDateTime endTime);
} 